package com.seeyon.apps.exportExcelCtrl.controller;

import com.seeyon.apps.exportExcelCtrl.manager.ExportExcelCtrlManager;
import com.seeyon.apps.exportExcelCtrl.units.ExportExcelCtrlUtil;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.util.Strings;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;

/**
 * @Title ExportExcelCtrlControllrt
 * @description: 领导数据导出自定义按钮controller
 * @author wxt.daicheng
 * @date 2024/8/20 
 * @version 1.0
 */
public class ExportExcelCtrlControllrt extends BaseController {

    private ExportExcelCtrlManager exportExcelCtrlManager;

    public void setExportExcelCtrlManager(ExportExcelCtrlManager exportExcelCtrlManager) {
        this.exportExcelCtrlManager = exportExcelCtrlManager;
    }

    /**
     * @title leader
     * @description: 打开领导选择页
     * @author wxt.daicheng
     * @param request:
     * @param response:
     * @date: 2024/8/20
     * @return: org.springframework.web.servlet.ModelAndView
     */
    public ModelAndView leader(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView modelAndView = new ModelAndView("apps/exportExcelCtrl/leader");
        return modelAndView;
    }

    /**
     * @title exportExcel
     * @description: 打开领导选择页
     * @author wxt.daicheng
     * @param request:
     * @param response:
     * @date: 2024/8/20
     * @return: org.springframework.web.servlet.ModelAndView
     */
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        String leader = request.getParameter("leader");
        try {
            if (Strings.isNotBlank(leader)){
                HashMap<String, Object> param = new HashMap<>();
                param.put("leader", leader);
                File file = exportExcelCtrlManager.exportExcel(param);

                ExportExcelCtrlUtil.download(response, file.getPath(),file.getName(),"utf-8");
            }
        } catch (Exception e) {
            ExportExcelCtrlUtil.log("导出数据发生异常", "领导数据导出", e);
        }

    }
}
